package com.accloud.cloudservice;

import com.accloud.common.ACConfiguration;
import com.accloud.service.ACException;
import com.accloud.service.ACFeedback;
import com.accloud.service.ACFeedbackMgr;
import com.accloud.service.ACFileInfo;
import com.accloud.service.ACMsg;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class ACFeedbackManager extends ACBaseManager implements ACFeedbackMgr {
    boolean isFail;
    private final Object lock = new Object();
    int uploadNum = 0;

    public ACFeedbackManager() {
        super(ACConfiguration.FEEDBACK_SERVICE_NAME, 1, false);
    }

    @Override // com.accloud.service.ACFeedbackMgr
    public void submitFeedback(final ACFeedback feedback, final VoidCallback callback) {
        if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            uploadFile(feedback, new VoidCallback() {
                /* class com.accloud.cloudservice.ACFeedbackManager.AnonymousClass1 */

                @Override // com.accloud.cloudservice.VoidCallback
                public void success() {
                    ACMsg req = ACMsg.getACMsg(feedback.getSubDomain());
                    req.setName("submitFeedback");
                    if (feedback.getType() != null) {
                        req.put("type", feedback.getType());
                    }
                    req.put("extend", feedback.getExtend());
                    ACFeedbackManager.this.sendReq(req, callback);
                }

                @Override // com.accloud.cloudservice.BaseCallback
                public void error(ACException e) {
                    callback.error(e);
                }
            });
        }
    }

    private void uploadFile(final ACFeedback feedback, final VoidCallback callback) {
        Map<String, List<byte[]>> pictureMap = feedback.getPictureMap();
        this.isFail = false;
        this.uploadNum = 0;
        if (pictureMap.keySet().size() == 0) {
            callback.success();
            return;
        }
        for (String key : pictureMap.keySet()) {
            for (byte[] picture : pictureMap.get(key)) {
                uploadFile(feedback, key, picture, new VoidCallback() {
                    /* class com.accloud.cloudservice.ACFeedbackManager.AnonymousClass2 */

                    @Override // com.accloud.cloudservice.VoidCallback
                    public void success() {
                        synchronized (ACFeedbackManager.this.lock) {
                            ACFeedbackManager.this.uploadNum++;
                            if (!ACFeedbackManager.this.isFail && ACFeedbackManager.this.uploadNum == feedback.getPictureNum()) {
                                callback.success();
                            }
                        }
                    }

                    @Override // com.accloud.cloudservice.BaseCallback
                    public void error(ACException e) {
                        synchronized (ACFeedbackManager.this.lock) {
                            if (!ACFeedbackManager.this.isFail) {
                                ACFeedbackManager.this.isFail = true;
                                callback.error(e);
                            }
                        }
                    }
                });
            }
        }
    }

    private void uploadFile(final ACFeedback feedback, final String key, byte[] data, final VoidCallback callback) {
        final ACFileInfo fileInfo = new ACFileInfo(true, "feedback", UUID.randomUUID().toString());
        fileInfo.setData(data);
        AC.fileMgr().uploadFile(fileInfo, null, new VoidCallback() {
            /* class com.accloud.cloudservice.ACFeedbackManager.AnonymousClass3 */

            @Override // com.accloud.cloudservice.VoidCallback
            public void success() {
                AC.fileMgr().getDownloadUrl(fileInfo, 0, new PayloadCallback<String>() {
                    /* class com.accloud.cloudservice.ACFeedbackManager.AnonymousClass3.AnonymousClass1 */

                    public void success(String url) {
                        feedback.addFeedbackPicture(key, url);
                        callback.success();
                    }

                    @Override // com.accloud.cloudservice.BaseCallback
                    public void error(ACException e) {
                        callback.error(e);
                    }
                });
            }

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException e) {
                callback.error(e);
            }
        });
    }
}
